<template>
  <main-layout>
    <div class="user-page">
      <div class="container">
        <div class="user-content">
          <!-- 侧边栏 -->
          <aside class="user-sidebar">
            <div class="user-info">
              <img
                :src="
                  $store.state.user.avatar ||
                  require('@/assets/images/avatar/avatar.svg')
                "
                :alt="$store.state.user.username"
                class="avatar"
              />
              <h3>{{ $store.state.user.username }}</h3>
              <p>{{ $store.state.user.phone }}</p>
            </div>
            <ul class="menu-list">
              <li
                v-for="menu in menus"
                :key="menu.id"
                :class="{ active: currentMenu === menu.id }"
                @click="currentMenu = menu.id"
              >
                <i :class="menu.icon"></i>
                {{ menu.name }}
              </li>
            </ul>
          </aside>

          <!-- 主内容区 -->
          <div class="main-content">
            <!-- 我的订单 -->
            <div v-if="currentMenu === 'orders'" class="orders-section">
              <h2>我的订单</h2>
              <div class="order-tabs">
                <span
                  v-for="status in orderStatuses"
                  :key="status.id"
                  :class="{ active: currentOrderStatus === status.id }"
                  @click="currentOrderStatus = status.id"
                >
                  {{ status.name }}
                </span>
              </div>
              <div class="order-list">
                <template v-if="filteredOrders.length === 0">
                  <div class="empty-tip">暂无订单</div>
                </template>
                <template v-else>
                  <div
                    v-for="order in filteredOrders"
                    :key="order.id"
                    class="order-item"
                  >
                    <div class="order-header">
                      <span class="order-number"
                        >订单号：{{ order.number }}</span
                      >
                      <span class="order-status">{{ order.statusText }}</span>
                    </div>
                    <div class="order-products">
                      <div
                        v-for="product in order.products"
                        :key="product.id"
                        class="product-item"
                      >
                        <img :src="product.image" :alt="product.name" />
                        <div class="product-info">
                          <h4>{{ product.name }}</h4>
                          <p>¥{{ product.price }} × {{ product.quantity }}</p>
                        </div>
                      </div>
                    </div>
                    <div class="order-footer">
                      <div class="order-total">
                        共{{ order.totalQuantity }}件商品 合计：¥{{
                          order.totalAmount
                        }}
                      </div>
                      <div class="order-actions">
                        <button
                          v-if="order.status === 'pending'"
                          class="btn-pay"
                        >
                          立即付款
                        </button>
                        <button
                          v-if="order.status === 'shipped'"
                          class="btn-confirm"
                        >
                          确认收货
                        </button>
                        <button
                          v-if="order.status === 'completed'"
                          class="btn-review"
                        >
                          评价
                        </button>
                        <button class="btn-detail">订单详情</button>
                      </div>
                    </div>
                  </div>
                </template>
              </div>
            </div>

            <!-- 收货地址 -->
            <div v-if="currentMenu === 'address'" class="address-section">
              <h2>收货地址</h2>
              <div class="address-list">
                <template v-if="addresses.length === 0">
                  <div class="empty-tip">暂无收货地址</div>
                </template>
                <template v-else>
                  <div
                    v-for="address in addresses"
                    :key="address.id"
                    class="address-item"
                  >
                    <div class="address-info">
                      <div class="contact">
                        <span class="name">{{ address.name }}</span>
                        <span class="phone">{{ address.phone }}</span>
                        <span v-if="address.isDefault" class="default-tag"
                          >默认</span
                        >
                      </div>
                      <div class="address-detail">
                        {{ address.province }}{{ address.city
                        }}{{ address.district }}{{ address.detail }}
                      </div>
                    </div>
                    <div class="address-actions">
                      <button class="btn-edit">编辑</button>
                      <button class="btn-delete">删除</button>
                      <button v-if="!address.isDefault" class="btn-set-default">
                        设为默认
                      </button>
                    </div>
                  </div>
                </template>
              </div>
              <button class="btn-add-address">
                <i class="fas fa-plus"></i>
                新增收货地址
              </button>
            </div>

            <!-- 账户设置 -->
            <div v-if="currentMenu === 'settings'" class="settings-section">
              <h2>账户设置</h2>
              <form class="settings-form" @submit.prevent="saveSettings">
                <div class="form-group">
                  <label>用户名</label>
                  <input
                    type="text"
                    v-model="settings.username"
                    placeholder="请输入用户名"
                  />
                </div>
                <div class="form-group">
                  <label>手机号</label>
                  <input
                    type="tel"
                    v-model="settings.phone"
                    placeholder="请输入手机号"
                  />
                </div>
                <div class="form-group">
                  <label>邮箱</label>
                  <input
                    type="email"
                    v-model="settings.email"
                    placeholder="请输入邮箱"
                  />
                </div>
                <div class="form-group">
                  <label>修改密码</label>
                  <input
                    type="password"
                    v-model="settings.oldPassword"
                    placeholder="请输入原密码"
                  />
                  <input
                    type="password"
                    v-model="settings.newPassword"
                    placeholder="请输入新密码"
                  />
                  <input
                    type="password"
                    v-model="settings.confirmPassword"
                    placeholder="请确认新密码"
                  />
                </div>
                <button type="submit" class="btn-save">保存修改</button>
              </form>
            </div>
          </div>
        </div>
      </div>
    </div>
  </main-layout>
</template>

<script>
import MainLayout from "@/components/layouts/MainLayout.vue";

export default {
  name: "UserView",
  components: {
    MainLayout,
  },
  data() {
    return {
      currentMenu: "orders",
      currentOrderStatus: "all",
      menus: [
        { id: "orders", name: "我的订单", icon: "fas fa-shopping-bag" },
        { id: "address", name: "收货地址", icon: "fas fa-map-marker-alt" },
        { id: "settings", name: "账户设置", icon: "fas fa-cog" },
      ],
      orderStatuses: [
        { id: "all", name: "全部" },
        { id: "pending", name: "待付款" },
        { id: "processing", name: "待发货" },
        { id: "shipped", name: "待收货" },
        { id: "completed", name: "已完成" },
      ],
      orders: [
        {
          id: 1,
          number: "DD20240101001",
          status: "pending",
          statusText: "待付款",
          products: [
            {
              id: 1,
              name: "浪漫玫瑰花束",
              price: 299,
              quantity: 1,
              image:
                "https://images.unsplash.com/photo-1494336956603-39a3641efa1c",
            },
          ],
          totalQuantity: 1,
          totalAmount: 299,
        },
      ],
      addresses: [
        {
          id: 1,
          name: "张三",
          phone: "13800138000",
          province: "广东省",
          city: "广州市",
          district: "天河区",
          detail: "珠江新城花城大道123号",
          isDefault: true,
        },
      ],
      settings: {
        username: "",
        phone: "",
        email: "",
        oldPassword: "",
        newPassword: "",
        confirmPassword: "",
      },
    };
  },
  computed: {
    filteredOrders() {
      if (this.currentOrderStatus === "all") {
        return this.orders;
      }
      return this.orders.filter(
        (order) => order.status === this.currentOrderStatus
      );
    },
  },
  created() {
    // 初始化设置表单
    const user = this.$store.state.user;
    if (user) {
      this.settings.username = user.username;
      this.settings.phone = user.phone;
      this.settings.email = user.email;
    }
  },
  methods: {
    saveSettings() {
      // 实现保存设置的逻辑
      console.log("保存设置:", this.settings);
    },
  },
};
</script>

<style scoped>
.user-page {
  padding: 40px 0;
  background: #f8f9fa;
  min-height: calc(100vh - 200px);
}

.user-content {
  display: grid;
  grid-template-columns: 280px 1fr;
  gap: 30px;
}

.user-sidebar {
  background: white;
  border-radius: 8px;
  overflow: hidden;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
}

.user-info {
  padding: 30px;
  text-align: center;
  border-bottom: 1px solid #eee;
}

.avatar {
  width: 100px;
  height: 100px;
  border-radius: 50%;
  margin-bottom: 15px;
}

.user-info h3 {
  margin-bottom: 5px;
  color: #333;
}

.user-info p {
  color: #666;
}

.menu-list {
  list-style: none;
}

.menu-list li {
  padding: 15px 30px;
  cursor: pointer;
  transition: all 0.3s;
  color: #666;
}

.menu-list li:hover {
  background: #f8f9fa;
  color: #ff6b6b;
}

.menu-list li.active {
  background: #ff6b6b;
  color: white;
}

.menu-list li i {
  margin-right: 10px;
  width: 20px;
}

.main-content {
  background: white;
  border-radius: 8px;
  padding: 30px;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
}

.main-content h2 {
  margin-bottom: 20px;
  padding-bottom: 15px;
  border-bottom: 1px solid #eee;
}

.order-tabs {
  display: flex;
  gap: 20px;
  margin-bottom: 20px;
}

.order-tabs span {
  padding: 8px 15px;
  cursor: pointer;
  color: #666;
  transition: all 0.3s;
}

.order-tabs span:hover {
  color: #ff6b6b;
}

.order-tabs span.active {
  color: #ff6b6b;
  border-bottom: 2px solid #ff6b6b;
}

.order-item {
  border: 1px solid #eee;
  border-radius: 8px;
  margin-bottom: 20px;
}

.order-header {
  padding: 15px;
  border-bottom: 1px solid #eee;
  display: flex;
  justify-content: space-between;
}

.order-status {
  color: #ff6b6b;
}

.order-products {
  padding: 15px;
}

.product-item {
  display: flex;
  gap: 15px;
  margin-bottom: 15px;
}

.product-item img {
  width: 80px;
  height: 80px;
  object-fit: cover;
  border-radius: 4px;
}

.product-info h4 {
  margin-bottom: 5px;
}

.order-footer {
  padding: 15px;
  border-top: 1px solid #eee;
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.order-actions {
  display: flex;
  gap: 10px;
}

.order-actions button {
  padding: 8px 15px;
  border: none;
  border-radius: 4px;
  transition: all 0.3s;
}

.btn-pay {
  background: #ff6b6b;
  color: white;
}

.btn-confirm {
  background: #ff6b6b;
  color: white;
}

.btn-review {
  background: #f8f9fa;
  color: #666;
}

.btn-detail {
  background: #f8f9fa;
  color: #666;
}

.address-item {
  border: 1px solid #eee;
  border-radius: 8px;
  padding: 20px;
  margin-bottom: 20px;
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.contact {
  margin-bottom: 10px;
}

.name {
  margin-right: 15px;
  font-weight: bold;
}

.default-tag {
  background: #ff6b6b;
  color: white;
  padding: 2px 8px;
  border-radius: 4px;
  font-size: 12px;
  margin-left: 10px;
}

.address-actions {
  display: flex;
  gap: 10px;
}

.address-actions button {
  padding: 6px 12px;
  border: 1px solid #ddd;
  background: white;
  border-radius: 4px;
  transition: all 0.3s;
}

.btn-edit:hover,
.btn-delete:hover,
.btn-set-default:hover {
  border-color: #ff6b6b;
  color: #ff6b6b;
}

.btn-add-address {
  width: 100%;
  padding: 15px;
  background: #f8f9fa;
  border: 1px dashed #ddd;
  border-radius: 8px;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 10px;
  transition: all 0.3s;
}

.btn-add-address:hover {
  border-color: #ff6b6b;
  color: #ff6b6b;
}

.settings-form {
  max-width: 500px;
}

.form-group {
  margin-bottom: 20px;
}

.form-group label {
  display: block;
  margin-bottom: 8px;
  color: #333;
}

.form-group input {
  width: 100%;
  padding: 10px;
  border: 1px solid #ddd;
  border-radius: 4px;
  margin-bottom: 10px;
}

.btn-save {
  width: 100%;
  padding: 12px;
  background: #ff6b6b;
  color: white;
  border: none;
  border-radius: 4px;
  transition: background 0.3s;
}

.btn-save:hover {
  background: #ff5252;
}

.empty-tip {
  text-align: center;
  padding: 40px;
  color: #999;
}

@media (max-width: 992px) {
  .user-content {
    grid-template-columns: 1fr;
  }

  .menu-list {
    display: flex;
    overflow-x: auto;
  }

  .menu-list li {
    padding: 15px 20px;
    white-space: nowrap;
  }
}

@media (max-width: 768px) {
  .order-footer {
    flex-direction: column;
    gap: 15px;
  }

  .order-actions {
    width: 100%;
    justify-content: flex-end;
  }

  .address-item {
    flex-direction: column;
    align-items: flex-start;
  }

  .address-actions {
    margin-top: 15px;
    width: 100%;
    justify-content: flex-end;
  }
}
</style> 